package com.huitone.gddw.controller.monitoring.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.ObjectDef;
import com.huitone.gddw.dao.entity.ObjectManagement;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.base.ObjectDefDTO;
import com.huitone.gddw.service.monitoring.base.IObjectDefService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 对象
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="objectDef")
public class ObjectDefController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(ObjectDefController.class);
	
	@Autowired
	private IObjectDefService objectDefService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<ObjectDefDTO> query(QueryPage<ObjectManagement> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询对象列表";
		QueryPage<ObjectDefDTO> result = null;
		try {
			page.setParams(getParams(request));
			result = objectDefService.selectPage(page);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return result;
		} catch (Exception e) {
			LOGGER.error("查询对象信息出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return result;
		}
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String objName = request.getParameter("objName");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("objName", objName);
		return params;
	}
	
	/**
	 * 获取所有对象信息
	 * @return List<ObjectManagement>
	 */
	@ResponseBody
	@RequestMapping(value="getAll")
	public List<ObjectManagement> getAll() {
		try {
			return objectDefService.getAll();
		} catch (Exception e) {
			LOGGER.error("获取所有对象信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 根据对象类型获取相应的对象
	 * @param objType 对象类型
	 * @return List<ObjectDef>
	 */
	@ResponseBody
	@RequestMapping(value="getByObjType")
	public List<ObjectDef> getByObjType(String objType) {
		try {
			return objectDefService.getByObjType(objType);
		} catch (Exception e) {
			LOGGER.error("根据对象类型获取相应的对象过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 准备修改页面的信息
	 * @param objId 对象ID
	 * @return ObjectDefDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public ObjectDefDTO toUpdate(String objId) {
		try {
			return objectDefService.findOne(objId);
		} catch (Exception e) {
			LOGGER.error("监控配置-对象管理-准备修改页面信息时出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param entity ObjectManagement
	 * @param request
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(ObjectDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加对象信息";
		try {
			objectDefService.save(entity);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存对象信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "保存失败！");
		}
	}
	
	/**
	 * 修改
	 * @param entity ObjectDef
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(ObjectDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改对象信息";
		try {
			ObjectDef objectDef = objectDefService.getById(entity.getObjId());
			objectDef.setObjName(entity.getObjName());
			objectDef.setObjType(entity.getObjType());
			objectDef.setRemark(entity.getRemark());
			objectDefService.update(objectDef);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "修改成功！");
		} catch (Exception e) {
			LOGGER.error("修改对象信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "修改失败！");
		}
	}
	
	/**
	 * 删除
	 * @param objId 对象ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String objId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除对象信息";
		try {
			objectDefService.delete(objId);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "删除成功！");
		} catch (Exception e) {
			LOGGER.error("删除对象信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "删除失败！");
		}
	}
	
}
